package com.googlecode.napp.pechhulp;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.widget.Button;
import android.widget.Toast;

public  class Pos extends Click{
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        /* Use the LocationManager class to obtain GPS locations */
        LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener mlocListener = new MyLocationListener();
        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 3,mlocListener);
        
        Button call_autopech = (Button) this.findViewById(R.id.call_autopech);
	    Button button = (Button) findViewById(R.id.check_status);

        call_autopech.setOnClickListener(this);
        button.setOnClickListener(this);        
        }
    
	public Pos() {
		Log.e("GPS_Loaded", "Checkpoint 4 calling");
	}
	
	static double latlocation = 0;
	static double lonlocation = 0;
	
    private void AlertMessageNoGps() {
	    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
	    builder.setMessage("U GPS staat uit, wilt u deze activeren?")
	           .setCancelable(false)
	           .setPositiveButton("Ja", new DialogInterface.OnClickListener() {
	               public void onClick(final DialogInterface dialog, final int id) {
	       	    	Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
	    	    	startActivityForResult(intent, RESULT_OK); 
	               }
	           })
	           .setNegativeButton("Nee", new DialogInterface.OnClickListener() {
	               public void onClick(final DialogInterface dialog, final int id) {
	                    dialog.cancel();
	               }
	           });
	    final AlertDialog alert = builder.create();
	    alert.show();
	}
		
	public class MyLocationListener implements LocationListener {
            @Override
            public void onLocationChanged(Location loc) {
                    latlocation = loc.getLatitude();
                    lonlocation = loc.getLongitude();
                    String Text = "My current location is: " + "Latitud = "+ latlocation + "Longitud = " + lonlocation;
                    Toast.makeText(getApplicationContext(), Text,Toast.LENGTH_SHORT).show();
                    Log.e("GPS", "location");
                    
                    
            }

            @Override
            public void onProviderDisabled(String provider) {
                    Toast.makeText(getApplicationContext(), "Gps Uitgeschakeld",Toast.LENGTH_SHORT).show();
                    Log.e(provider, provider);
                    //Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); // Direct naar GPS instellingen, vervangen door question box
                    //startActivityForResult(intent, RESULT_OK);
                    AlertMessageNoGps();
                    //Toast.makeText(getApplicationContext(), "Zet de GPS functie aan AUB, Druk daarna op de terug toets", Toast.LENGTH_LONG).show();

            }

            @Override
            public void onProviderEnabled(String provider) {
                    Toast.makeText(getApplicationContext(), "Gps Ingeschakeld",Toast.LENGTH_SHORT).show();
                    Log.e("GPS","Ingeschakeld");
                    
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
            }
            
            
    }/* End of Class MyLocationListener */

		
}